#ifndef Q_TCPCLIENT_H
#define Q_TCPCLIENT_H
#include <QObject>
#include <QTcpSocket>

class TCPClient : public QObject
{
    Q_OBJECT
  public:
    explicit TCPClient(QObject *parent = nullptr);

    /* 外界调用这三个函数即可 */
    void connectToServer(const QString &host, quint16 port);
    void disconnectFromServer();
    void sendMessage(const QByteArray &data);

  signals:
    /* 供 UI 层订阅 */
    void connected();
    void disconnected();
    void errorOccurred(const QString &msg);
    void messageReceived(const QByteArray &msg);
    void bytesWritten(qint64 bytes);

  private slots:
    void onConnected();
    void onDisconnected();
    void onReadyRead();
    void onError(QAbstractSocket::SocketError err);

  private:
    QTcpSocket *m_socket = nullptr;
};
#endif // Q_TCPCLIENT_H